package com.ymall.common;


import com.ymall.util.PropertiesUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisPool {

    private static JedisPool pool;

    private static Integer maxTotal = Integer.parseInt(PropertiesUtil.getProperty("","10"));

    private static Integer maxIdel = Integer.parseInt(PropertiesUtil.getProperty("","8"));

    private static Integer minIdel = Integer.parseInt(PropertiesUtil.getProperty("","2"));

    private static String ip = PropertiesUtil.getProperty("redis.ip","127.0.0.1");

    private static Integer port = Integer.parseInt(PropertiesUtil.getProperty("redis.port","6379"));

    private static void initPool(){

        JedisPoolConfig config = new JedisPoolConfig();

        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdel);
        config.setMinIdle(minIdel);

        config.setTestOnBorrow(false);
        config.setTestOnReturn(false);

        pool = new JedisPool(config,ip,port,1000*2);
    }

    static {
        initPool();
    }

    public static Jedis getJedis(){
        return pool.getResource();
    }


    public static void returnBrokenResource(Jedis jedis){
        pool.returnBrokenResource(jedis);
    }

    public static void returnResource(Jedis jedis){
        pool.returnResource(jedis);
    }

}
